import {_decorator, Component, Node, Prefab, tween} from 'cc';
import {BaseInfo} from "db://assets/scripts/data/model/BaseInfo";
import {AudioManager} from "db://assets/scripts/common/AudioManager";
import {ResourceUtils} from "db://assets/scripts/common/ResourceUtils";
import {GameManager} from "db://assets/scripts/game/GameManager";
import {PoolManager} from "db://assets/scripts/common/PoolManager";
import {EffectManager} from "db://assets/scripts/common/EffectManager";
import {Skill} from "db://assets/scripts/data/model/Skill";
import {AudioPaths} from "db://assets/scripts/common/AudioPaths";
import {WarningCircleEffect} from "db://assets/scripts/game/effect/warningSkill/WarningCircleEffect";
import {WarningLineEffect} from "db://assets/scripts/game/effect/warningSkill/WarningLineEffect";
import {MonsterController} from "db://assets/scripts/game/role/monster/MonsterController";
import {WarningStripEffect} from "db://assets/scripts/game/effect/warningSkill/WarningStripEffect";

const {ccclass, property} = _decorator;

/**
 * 大火球脚本: 炸开的时候才有伤害，跟小火球一样
 */
@ccclass('FireBallBigEffect')
export class FireBallBigEffect extends Component {
    /**
     * 预警技能脚本
     */
    public warningScript: WarningCircleEffect | WarningLineEffect | WarningStripEffect;
    /**
     * 是否开始播放爆炸特效
     */
    public isPlayHitFireBall: boolean = false;
    /**
     * 技能信息
     */
    public skill: Skill;
    /**
     * 敌人基本信息
     */
    public baseInfo: BaseInfo;

    /**
     * 初始化
     *
     * @param skill 技能信息
     * @param baseInfo 基础信息
     * @param parentScript 使用者
     */
    public init(skill: Skill, baseInfo: any, parentScript?: MonsterController) {
        this.skill = skill;
        this.baseInfo = baseInfo;
        this.isPlayHitFireBall = false;

        let playerWorldPosition = parentScript.attackPosition;
        this.node.setWorldPosition(playerWorldPosition.x, 23, playerWorldPosition.z);

        this.node.children.forEach((ndChild: Node) => {
            ndChild.active = true;
        })

        EffectManager.getInstance().playParticle(this.node);

        tween(this.node)
            .to(1 / skill.speed, {position: playerWorldPosition}, {easing: "elasticIn"})
            .call(() => {
                AudioManager.getInstance().playSound(AudioPaths.fireBallBigSound);

                this.isPlayHitFireBall = true;
                //关闭预警
                parentScript?.warningScript?.hideWarning();

                this.node.children.forEach((childNode: Node) => {
                    childNode.active = false;
                })

                ResourceUtils.loadEffect("hit/hitFireBall2").then((prefab: Prefab) => {
                    let effectNode: Node = PoolManager.getInstance().getNode(prefab, this.node);
                    effectNode.setWorldPosition(this.node.worldPosition);

                    EffectManager.getInstance().playParticle(effectNode,
                        GameManager.getInstance().gameSpeedProxy.getGameSpeed(),
                        true, 1.1, () => {
                            PoolManager.getInstance().putNode(effectNode);
                            PoolManager.getInstance().putNode(this.node);
                        });
                })
            })
            .start()
    }
}